package org.wanxian.stores.db;

import java.util.ArrayList;

import org.wanxian.stores.model.Catalog;
import org.wanxian.stores.model.Product;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CatalogDao {
	private DatabaseOpenHelper dbHelper;
	private static final String TABLE_NAME = "catalog";
	
	public CatalogDao(Context context) {
		dbHelper = new DatabaseOpenHelper(context);
	}
	
	public int insert(Catalog catalog) {
		Catalog bef = queryById(catalog.getId());
		if(bef != null) {
			deleteById(bef.getId());
		}
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", catalog.getName());
		values.put("storeId", catalog.getStoreId());
		int id = (int) db.insert(TABLE_NAME, null, values);
		return id;
	}
	
	public void deleteById(int id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE_NAME, "id = ?", new String[] {String.valueOf(id)});
		db.close();
	}
	public Catalog queryById(int id) {
		Catalog catalog = null;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "id = ?", new String[]{String.valueOf(id)}, null, null, null);
		while(cursor.moveToNext()) {
			String name = cursor.getString(cursor.getColumnIndex("name"));
			int storeId = cursor.getInt(cursor.getColumnIndex("storeId"));
			catalog = new Catalog(id, storeId, name);
		}
		cursor.close();
		db.close();
		return catalog;
	}
	public Catalog queryByName(String name) {
		Catalog catalog = null;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "name = ?", new String[]{name}, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			int storeId = cursor.getInt(cursor.getColumnIndex("storeId"));
			catalog = new Catalog(id, storeId, name);
		}
		cursor.close();
		db.close();
		return catalog;
	}
	
	public ArrayList<Catalog> queryAllByStoreId(int storeId) {
		ArrayList<Catalog> list = new ArrayList<Catalog>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "storeId = ?", new String[]{String.valueOf(storeId)}, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			Catalog catalog =  new Catalog(id, storeId, name);
			list.add(catalog);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	
	public ArrayList<Catalog> queryAll() {
		ArrayList<Catalog> list = new ArrayList<Catalog>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			int storeId = cursor.getInt(cursor.getColumnIndex("storeId"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			Catalog catalog =  new Catalog(id, storeId, name);
			list.add(catalog);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	
	public int getCount() {
		return queryAll().size();
	}
}
